package com.liyu.listener;

import cn.hutool.core.date.DateUtil;
import com.liyu.config.DelayedComment;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Date;

/**
 * @author 李誉
 * @date 2022-07-17 15:30
 * @description 延迟队列监听消息
 */
@Slf4j
@Component
public class DelayedConsumer {

    @RabbitListener(queues = DelayedComment.DELAYED_QUEUE)
    public void consume(Message message, Channel channel) throws IOException {

        String msg = new String(message.getBody(), StandardCharsets.UTF_8);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        log.info("延迟队列{}于{}消费了一条消息：{}", message.getMessageProperties().getConsumerQueue(), DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"), msg);
    }

}
